package org.example.panel.scorepanel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import org.example.operation.*;

/**
 * @author lenovo
 */

public class SearchNameScorePanel extends JPanel {
    public SearchNameScorePanel(){
        //组件准备
        JTextField name = new JTextField(15);
        JButton searchNameScore = new JButton("按姓名查找");
        Box nameBox = Box.createHorizontalBox();
        Box box = Box.createVerticalBox();
        JTextArea textArea = new JTextArea(15,10);
        JScrollPane scrollPane = new JScrollPane(textArea);
        scrollPane.setPreferredSize(new Dimension(300,230));
        //注册监视器
        searchNameScore.addActionListener((ActionEvent e) -> {
            if(name.getText().matches(".*\\s+.*")){
                JOptionPane.showMessageDialog(null,
                        "姓名不能有空格", null, JOptionPane.WARNING_MESSAGE);
            }else{
                String string = ScoreOperation.searchNameScore(name.getText());
                if(string.isEmpty()){
                    JOptionPane.showMessageDialog(null,
                            "找不到学生", null, JOptionPane.WARNING_MESSAGE);
                }else{
                    textArea.setText(string);
                }
            }
        });
        //添加组件
        nameBox.add(new JLabel("姓名： "));
        nameBox.add(name);
        nameBox.add(Box.createHorizontalStrut(5));
        nameBox.add(searchNameScore);
        box.add(nameBox);
        box.add(Box.createVerticalStrut(5));
        textArea.setEditable(false);
        box.add(scrollPane);
        add(box);
    }
}
